package nan.demo;

import javax.swing.*;
import java.sql.*;

public class DBtest {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root"; // 你的数据库用户名
        String password = "060625nan"; // 你的数据库密码
        String sql="select * from user where username=? and password=?";

        int id=0;
        String email=null;

        Connection conn=null;
        PreparedStatement prep=null;
        ResultSet rs=null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
             conn=DriverManager.getConnection(url, username, password);

            String name="admin";
            String pass="admin123";


            //数据查询
             prep = conn.prepareStatement(sql);
            prep.setString(1,name);
            prep.setString(2,pass);


            //数据结果集
             rs = prep.executeQuery();

            while (rs.next()) {
                 id = rs.getInt("id");
                email = rs.getString("email");
            }


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                System.out.println("rs");
                throw new RuntimeException(e);
            }
            try {
                prep.close();
            } catch (SQLException e) {
                System.out.println("prep");
                throw new RuntimeException(e);
            }
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("conn");
                throw new RuntimeException(e);
            }
        }


        System.out.println(id);
        System.out.println(email);

    }
}
